package com.ruoyi.web.controller.crm;

import com.bonuo.crm.domain.Agent;
import com.bonuo.crm.service.AgentService;
import com.bonuo.crm.service.ContactService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

//代理商controller
@RestController
@RequestMapping("/crm/agent")
@Api(tags = "合作伙伴管理")
public class AgentController extends BaseController {

    @Autowired
    private AgentService agentService;
    @Autowired
    private ContactService contactService;


    //代理商列表
    @ApiOperation(value = "查询合作伙伴记录",httpMethod = "GET")
    @GetMapping("/list")
    public TableDataInfo list(Agent agent,Integer pageNum,Integer pageSize){
        if(pageNum != null && pageSize != null){
            startPage();
        }
        List<Agent> agentList = agentService.selectAgentList(agent);
        return getDataTable(agentList);
    }

    //代理商新增
    @ApiOperation(value = "添加合作伙伴数据",httpMethod = "POST")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Agent agent){
        //查询客户是否存在根据税号
        String taxNumber = agentService.selectClientByTaxNumber(agent);
        if(taxNumber != null && !taxNumber.equals("")){
            return AjaxResult.error("统一社会信用代码已存在");
        }
        Integer add = agentService.insertAgent(agent);
        return toAjax(add);
    }

    //代理商删除
    @ApiOperation(value = "删除合作伙伴数据",httpMethod = "DELETE")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Integer id){
        //查询当前代理商下是否有联系人信息
        Integer countContact = contactService.selectContactByFacultyOrAgentId(id,1);
        if(countContact >0) {
            return error("该合作伙伴下含有联系人信息，请删除所有联系人信息重试");
        }
        Integer delete = agentService.deleteAgentById(id);
        return toAjax(delete);
    }

    //代理商修改
    @ApiOperation(value = "修改合作伙伴数据",httpMethod = "PUT")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody Agent agent){
        //查询客户是否存在根据税号
        String taxNumber = agentService.selectClientByTaxNumber(agent);
        if(taxNumber != null && !taxNumber.equals("")){
            return AjaxResult.error("统一社会信用代码已存在");
        }
        Integer update = agentService.updateAgent(agent);
        return toAjax(update);
    }

    //代理商导出
    @ApiOperation(value = "导出代理商数据",httpMethod = "POST")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Agent agent) {
        List<Agent> list = agentService.selectAgentList(agent);
        ExcelUtil<Agent> util = new ExcelUtil<Agent>(Agent.class);
        util.exportExcel(response, list, "代理商数据");
    }

}
